package com.net.framework.help.utils;

import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.widget.EditText;

/***
 * 读取验证码短信
 *
 * @author zhangxiaobin
 */
public class SMSContentObserverUtil extends ContentObserver {
    private Activity activity;
    private String SmsContent;
    private EditText verifyCodeEditText;
    private String sendNumber;
    private String smsTag;
    private String str_start;
    private String str_end;

    /***
     * @param paramEditText 验证码编辑框
     * @param sendNumber    发送者号码 （"1065%"）
     * @param smsTag        短信标签
     * @param str_start     验证码起始字符截取位置
     * @param str_end       验证码结束字符截取位置
     */
    public SMSContentObserverUtil(Activity paramActivity, Handler handler, EditText paramEditText, String sendNumber, String smsTag, String str_start, String str_end) {
        super(handler);
        this.activity = paramActivity;
        this.verifyCodeEditText = paramEditText;
        this.sendNumber = sendNumber;
        this.smsTag = smsTag;
        this.str_start = str_start;
        this.str_end = str_end;
    }

    @Override
    public void onChange(boolean paramBoolean) {
        super.onChange(paramBoolean);
        try {
            @SuppressWarnings("deprecation")
            Cursor localCursor = this.activity.managedQuery(Uri.parse("content://sms/inbox"), new String[]{"_id", "address", "body", "read"}, "address like ? and read=?", new String[]{sendNumber, "0"}, "date desc");
            if (localCursor != null) {
                localCursor.moveToFirst();
                if (localCursor.moveToFirst()) {
                    // 获取短信内容
                    String str = localCursor.getString(localCursor.getColumnIndex("body"));
                    if (str.contains(smsTag)) {
                        SmsContent = StringUtil.cuttingBetween(str, str_start, str_end);
                        this.verifyCodeEditText.setText(SmsContent);
                        this.verifyCodeEditText.setSelection(SmsContent.length());
                        return;
                    } else {
                        this.verifyCodeEditText.setText("");
                        return;
                    }
                }
            }
        } catch (Exception localException) {
        }
    }
}
